﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Player : MonoBehaviour {

	public enum PlayerType{
		Conservative,
		Average,
		Offenssive
	}
	public int PlayerID;
	public PlayerType playerType;

	[Serializable]
	public class PlayerCurrentState
	{
		public float ResourceInStore;
		public int EnemySystemCount;
		public int OwnedSystems;
		[HideInInspector]
		public float MaxRecource{
			get{ return OwnedSystems * 1000f;}
		}
	}
	[SerializeField]
	public PlayerCurrentState CurState;

	UnityEngine.Object EventLine;
	void Awake(){
		EventLine = Resources.Load ("Prefab/EventLine");
	}

	[Space(10)][Header("Value")]
	public GameObject Start;
	public GameObject End;

	public void Action(Finder.FindType Type)
	{
		//GameObject Start;
		//GameObject End;
		Start = Finder.Instance.FindOneOfMine (PlayerID);
		End = Finder.Instance.FindTheSystem (Start,Type,ControlCenter.Instance.FindTimes,PlayerID);
		if (End == null) {
			//GetComponent<PlayerAcion>().RePrePare (Type);
			return;
		}

		float Dis;
		Dis = (Start.transform.position - End.transform.position).magnitude;

		if (!JudgeCost (Type, Dis))
			return;

		GameObject clone;
		clone = Instantiate (EventLine) as GameObject;
		clone.GetComponent<EventLine> ().Player = PlayerID;
		clone.GetComponent<EventLine> ().Starts = Start;
		clone.GetComponent<EventLine> ().Target = End;
		clone.GetComponent<EventLine> ().type = Type;
		clone.GetComponent<EventLine> ().Proceed_Total = Dis*ControlCenter.Instance.speedAndTime.LightSpeed;
	}
	bool JudgeCost(Finder.FindType type,float dis){
		float cost;
		switch (type) 
		{
		case Finder.FindType.explore:
			cost = dis * ControlCenter.Instance.baseResourceUse.Explore;
			break;			
		case Finder.FindType.colony:
			cost = dis * ControlCenter.Instance.baseResourceUse.Colony;
			break;			
		case Finder.FindType.enemy:
			cost = dis * ControlCenter.Instance.baseResourceUse.Destroy;
			break;
		default:
			cost = 0f;
			break;
		}

		if (CurState.ResourceInStore - cost < 0f)
			return false;

		CurState.ResourceInStore -= cost;
		return true;
	}

}
